package
{
	import com.fminutes.vinciBase.App;
	import com.fminutes.vinciIsometric.events.IsoObjectMouseEvent;
	import com.fminutes.vinciUI.containers.RollContainer;
	import com.fminutes.vinciUI.controls.ScaleButton;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import iso.CityIsoWorld;
	import iso.PlayerIsoObject;
	import iso.WallIsoObject;
	
	import log.Logger;
	
	import utils.tostring.LeftTopProperty;
	
	[SWF(width="720", height="650", backgroundColor=0xEEEEEE)]
	public class Main extends Sprite
	{
		public static const ISO_WORLD_X_NUM:Number = 15;
		public static const ISO_WORLD_Z_NUM:Number = 15;
		public static const ISO_WORLD_SIZE:Number = 32;
		public static const ISO_WORLD_SHOW_NUM:Number = 1;
		
		protected var _cityIsoWorld:CityIsoWorld;
		protected var _rollContainer:RollContainer;
		private var _mousePosition:Point;
		private var _player:PlayerIsoObject;
		private var _isDIYState:Boolean;
		private var _DIYScaleButton:ScaleButton;
		private var _deleteScaleButton:ScaleButton;
		/**
		 *是否正在拖动场景 
		 */
		private var _isRollingRollContainer:Boolean;
		private var _isDeleteScaleButtonClicked:Boolean;
		private var _diyIsoObject:WallIsoObject;
		
		public function Main()
		{
			initFramework();
			initView();
			initListener();
		}
		
		private function initFramework():void
		{
			App.appStage = stage;
		}
		
		private function initView():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			_cityIsoWorld = new CityIsoWorld(ISO_WORLD_X_NUM, ISO_WORLD_Z_NUM, ISO_WORLD_SIZE, ISO_WORLD_SHOW_NUM);
			_rollContainer = new RollContainer();
			_player = PlayerIsoObject.produceRandomCubePlayer();
			_DIYScaleButton = getScaleButton("diy");
			_deleteScaleButton = getScaleButton("delete");
			
			_DIYScaleButton.x = 20;
			_DIYScaleButton.y = 20;
			
			_deleteScaleButton.x = _DIYScaleButton.x;
			_deleteScaleButton.y = _DIYScaleButton.y + _DIYScaleButton.height;
			_deleteScaleButton.visible = false;
			
			_rollContainer.showRect = new Rectangle(-(_cityIsoWorld.width / 2), -50, _cityIsoWorld.width, _cityIsoWorld.height);
			_rollContainer.width = stage.stageWidth;
			_rollContainer.height = stage.stageHeight;
			_rollContainer.reRoll();
		
			_cityIsoWorld.isoGridVisible = true;
			
			_cityIsoWorld.addToWorld(_player);
			_rollContainer.addChild(_cityIsoWorld);
			stage.addChild(_rollContainer);
			stage.addChild(_DIYScaleButton);
			stage.addChild(_deleteScaleButton);
		}
		
		private function initListener():void
		{
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			
			_DIYScaleButton.addEventListener(MouseEvent.CLICK, onDIYScaleButtonClick);
			_deleteScaleButton.addEventListener(MouseEvent.CLICK, onDeleteScaleButtonClick);
			
			_cityIsoWorld.addEventListener(IsoObjectMouseEvent.MOUSE_CLICK, onIsoObjectMouseClick);
		}

		private function onIsoObjectMouseClick(event:IsoObjectMouseEvent):void
		{
			if (event.eventObject is WallIsoObject) 
			{
				if (_isDeleteScaleButtonClicked) 
				{
					_cityIsoWorld.removeWallWithWallIsoObject(event.eventObject as WallIsoObject);
					_isDeleteScaleButtonClicked = false;
				}else
				{
					_diyIsoObject = event.eventObject as WallIsoObject;
					_diyIsoObject.startDrag();
				}
			}
		}

		private function onDeleteScaleButtonClick(event:MouseEvent):void
		{
			_isDeleteScaleButtonClicked = true;
		}

		private function onMouseMove(event:MouseEvent):void
		{
			if (event.buttonDown) 
			{
				//拖动rollcontainer
				_isRollingRollContainer = true;
				_rollContainer.left += (_mousePosition.x - mouseX);
				_rollContainer.top += (_mousePosition.y - mouseY);
				_rollContainer.reRoll();
				updateMousePosition();
				Logger.debug(LeftTopProperty(_rollContainer));
			}	
		}
		
		/**
		 *拖动rollcontainer的过程中更新鼠标的坐标 
		 * 
		 */
		private function updateMousePosition():void
		{
			_mousePosition.x = mouseX;
			_mousePosition.y = mouseY;
		}		
		
		private function onMouseDown(event:MouseEvent):void
		{
			//记录鼠标的坐标 拖动场景是使用
			_mousePosition = new Point(mouseX, mouseY);
		}
		
		private function onMouseUp(event:MouseEvent):void
		{
			//TODO拖动场景的时候不能添加建筑或寻路
			if (_isRollingRollContainer)
			{
				_isRollingRollContainer = false;
				return;
			}
			
			//正在拖动建筑物
			if (_diyIsoObject != null)
			{
				_diyIsoObject.stopDrag();
				_cityIsoWorld.removeWallWithWallIsoObject(_diyIsoObject);
				_cityIsoWorld.addWall(_diyIsoObject, _cityIsoWorld.getMousePoint());
				_diyIsoObject = null;
				return;
			}
			
			if (_isDIYState) 
			{
				if (!_cityIsoWorld.hasIsoObjectUnderPosition(_cityIsoWorld.getMousePosition())) 
				{
					_cityIsoWorld.addWall(WallIsoObject.produceRandomCubeWall(), _cityIsoWorld.getMousePoint());
				}else
				{
					_cityIsoWorld.executeClick(event);
				}
			}
			else
			{
				//寻路，移动
				_player.stop();
				_player.move(_cityIsoWorld);
			}
		}
		
		private function getScaleButton(str:String):ScaleButton
		{
			var textField:TextField = new TextField();
			textField.text = str;
			return new ScaleButton(textField);
		}
		
		private function onDIYScaleButtonClick(event:MouseEvent):void
		{
			if (_isDIYState) 
			{
				_isDIYState = false;
				_deleteScaleButton.visible = false;
			}
			else
			{
				_isDIYState = true;
				_deleteScaleButton.visible = true;
			}
		}
	}
}